﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace tmAsk.Data
{

    public class MakehtmlSQL
    {
        // private string Sqltype = Make_helper.ConnectionType;

        public int Add(string sql)
        {
            object single = MakeSQL.GetSingle(sql);
            if (single != null)
            {
                single = Convert.ToInt32(single);
            }
            return 0;
        }

        public int Add(string sql, SqlParameter[] cmdpara)
        {
            object single = MakeSQL.GetSingle(sql, cmdpara);
            if (single != null)
            {
                single = Convert.ToInt32(single);
            }
            return 0;
        }

        public int ExecuteSql(string sql)
        {
            return MakeSQL.ExecuteSql(sql);
        }

        public int ExecuteSql(string sql, SqlParameter[] cmdparm)
        {
            return MakeSQL.ExecuteSql(sql, cmdparm);
        }

        public DataSet GetList(string sql)
        {
            return MakeSQL.Query(sql.ToString());
        }

        public DataSet GetList(string sql, SqlParameter[] cmdpara)
        {
            return MakeSQL.Query(sql.ToString(), cmdpara);
        }

        public DataSet GetList(string tablename, string strWhere, string filename)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select " + filename);
            builder.Append(" from " + tablename);
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return MakeSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string tablename, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" * ");
            builder.Append(" FROM " + tablename);
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            if (filedOrder.Trim() != "")
            {
                builder.Append(" order by " + filedOrder);
            }
            return MakeSQL.Query(builder.ToString());
        }

        public DataSet GetList(string tblName, string fldName, int PageSize, int PageIndex, string orderfldName, int OrderType, string strWhere)
        {
            string str2;
            string str3;
            string str4;
            string str = "*";
            if (OrderType == 0)
            {
                str2 = " > (select max(" + fldName + ") from ( select top ";
                str3 = " order by " + orderfldName;
            }
            else
            {
                str2 = " < ( select min(" + fldName + ") from (select top ";
                str3 = " order by " + orderfldName;
            }
            if (PageIndex == 1)
            {
                if (strWhere == "")
                {
                    str4 = string.Concat(new object[] { "Select top ", PageSize, " ", str, " from ", tblName, str3 });
                }
                else
                {
                    str4 = string.Concat(new object[] { "Select top ", PageSize, " ", str, " from ", tblName }) + " where " + strWhere + str3;
                }
            }
            else
            {
                object obj2;
                if (strWhere == "")
                {
                    obj2 = string.Concat(new object[] { "Select top ", PageSize, " ", str, " from ", tblName });
                    str4 = string.Concat(new object[] { obj2, " where ", fldName, str2, (PageIndex - 1) * PageSize, " ", fldName, " from ", tblName }) + str3 + ") as maxid )" + str3;
                }
                else
                {
                    obj2 = string.Concat(new object[] { "Select top ", PageSize, " ", str, " from ", tblName });
                    string str5 = string.Concat(new object[] { obj2, " where ( ", fldName, str2, (PageIndex - 1) * PageSize, " ", fldName, " from ", tblName });
                    str4 = str5 + " where " + strWhere + " " + str3 + ") as maxid ) ) and ( " + strWhere + " )" + str3;
                }
            }
            return MakeSQL.Query(str4.ToString());
        }

        public int GetListcount(string sql)
        {
            return Convert.ToInt32(MakeSQL.Query(sql).Tables[0].Rows[0][0]);
        }

        public int GetListcount(string tblName, string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(*)");
            builder.Append(" FROM " + tblName);
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return Convert.ToInt32(MakeSQL.Query(builder.ToString()).Tables[0].Rows[0][0]);
        }

        public int GetListcount(string key, string tblName, string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select " + key);
            builder.Append(" FROM " + tblName);
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return Convert.ToInt32(MakeSQL.Query(builder.ToString()).Tables[0].Rows[0][0]);
        }
    }
}

